package utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;


/**
 * @author yangzhi
 * @date 2018/9/5
 * @FastJson 相关方法
 */
public class JSONObjectUtils {

	/**
	 * 处理list
	 * @param jsonArray
	 * @return
	 */
	public static JSONObject get(JsonArray jsonArray,Integer index){
		JSONObject result = new JSONObject();
		if (jsonArray.size()>0){
			result = JSONObject.parseObject(jsonArray.get(index).toString());
		}
		return  result;
	}


	/**
	 * 处理data 中的 map
	 * @param data
	 * @param key
	 * @return
	 */
	public static JSONObject get(JsonObject data,String key){
		JSONObject result = new JSONObject();
		if (data.get(key)!=null){
			result =JSONObject.parseObject(data.get(key).toString());
		}
		return result;
	}


	public static JSONObject getJSONObject(JSONObject jsonObject,String jsonKey){
		JSONObject result = new JSONObject();
		Object object =jsonObject.get(jsonKey);
		if (object!=null){
			result = JSONObject.parseObject(object.toString());

		}
		return  result;
	}

	public static String getString(JSONObject jsonObject,String jsonKey,String value){
		String result = "";
		Object object =jsonObject.get(jsonKey);
		if (object!=null){
			JSONObject tmp = JSONObject.parseObject(object.toString());
			if (tmp != null) {
				result = tmp.getString(value);
			}
		}
		return  result;
	}

	public static Integer getInteger(JSONObject jsonObject,String jsonKey,String value){
		Integer result = 0;
		Object object =jsonObject.get(jsonKey);
		if (object!=null){
			JSONObject tmp = JSONObject.parseObject(object.toString());
			if (tmp != null) {
				result =Integer.parseInt(tmp.getString(value));
			}
		}
		return  result;
	}

	public static Long getLong(JSONObject jsonObject,String jsonKey,String value){
		Long result = 0L;
		Object object =jsonObject.get(jsonKey);
		if (object!=null){
			JSONObject tmp = JSONObject.parseObject(object.toString());
			if (tmp != null) {
				result =Long.parseLong(tmp.getString(value));
			}
		}
		return  result;
	}
}
